#include "TeNetMgr.h"

#include "SocketMgrLinux.h"
#include "ListenSocketLinux.h"


#define NET_CONNECT() SocketMgr& rkSocketMgr = *((SocketMgr*)m_pkData)

TeNetMgr::TeNetMgr()
{
    m_pkData = NULL;
}
TeNetMgr::~TeNetMgr()
{
    NET_CONNECT();
    
}

bool TeNetMgr::Init(TeNetCall* pkCall,int iThreadCount)
{
    m_pkData = new SocketMgr(pkCall);
    NET_CONNECT();
    if(!rkSocketMgr.Init())
    {
        return false;
    }
    rkSocketMgr.SpawnWorkerThreads();

}
void TeNetMgr::Update()
{
    NET_CONNECT();
    rkSocketMgr.Update();
}
bool TeNetMgr::StartListen(const char* pcIP,int iPort,int iSendBufferSize,int iRecvBufferSize)
{
    NET_CONNECT();
    ListenSocket<Socket>* ls = new ListenSocket<Socket>(iSendBufferSize,iRecvBufferSize);
    ls->SetSocketMgr(&rkSocketMgr);
    ls->StartListen(pcIP, iPort);
    bool bRet = ls->IsOpen();
    if(bRet)
    {
        return true;
    }
    return false;
}
bool TeNetMgr::Connect(const char* pcIP,int iPort,int iSendBufferSize,int iRecvBufferSize)
{
    NET_CONNECT();
    Socket * s = ConnectTCPSocket<Socket>(&rkSocketMgr,pcIP, iPort,iSendBufferSize,iRecvBufferSize);
    if(!s)
    {
        return false;
    }
    return true;
}

bool TeNetMgr::SendData(HSock& rkSock,void* pData,int iSize)
{
    NET_CONNECT();
    return rkSocketMgr.SendBuffer((long)(rkSock.pkData),pData,iSize);
}
void TeNetMgr::DisConnect(HSock& rkSock)
{
    NET_CONNECT();
    rkSocketMgr.DisConnect((long)(rkSock.pkData));

}
